package com.xiaomi.shoppinglist

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.RemoteViews
import android.util.Log

class ShoppingListWidget : AppWidgetProvider() {
    private val TAG = "ShoppingListWidget"

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        Log.d(TAG, "onUpdate called")
        // 为每个小部件实例更新
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId)
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds)
    }

    override fun onEnabled(context: Context) {
        // 当第一个小部件实例被创建时调用
        Log.d(TAG, "onEnabled called")
    }

    override fun onDisabled(context: Context) {
        // 当最后一个小部件实例被删除时调用
        Log.d(TAG, "onDisabled called")
    }

    companion object {
        fun updateAppWidget(
            context: Context,
            appWidgetManager: AppWidgetManager,
            appWidgetId: Int
        ) {
            Log.d("ShoppingListWidget", "updateAppWidget called for id: $appWidgetId")

            // 设置一次性购物项列表
            val oneTimeIntent = Intent(context, OneTimeListService::class.java)
            oneTimeIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
            oneTimeIntent.data = Uri.parse(oneTimeIntent.toUri(Intent.URI_INTENT_SCHEME))

            // 设置可重复购物项列表
            val repeatableIntent = Intent(context, RepeatableListService::class.java)
            repeatableIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
            repeatableIntent.data = Uri.parse(repeatableIntent.toUri(Intent.URI_INTENT_SCHEME))

            // 构建 RemoteViews
            val views = RemoteViews(context.packageName, R.layout.shopping_list_widget)

            // 设置一次性购物项列表
            views.setRemoteAdapter(R.id.one_time_list, oneTimeIntent)
            views.setEmptyView(R.id.one_time_list, R.id.empty_view_one_time)

            // 设置可重复购物项列表
            views.setRemoteAdapter(R.id.repeatable_list, repeatableIntent)
            views.setEmptyView(R.id.repeatable_list, R.id.empty_view_repeatable)

            // 设置添加按钮点击事件
            val addIntent = Intent(context, AddItemActivity::class.java)
            val addPendingIntent = PendingIntent.getActivity(
                context, 0, addIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
            views.setOnClickPendingIntent(R.id.add_button, addPendingIntent)

            // 设置列表项点击事件
            val itemClickIntent = Intent(context, ShoppingListWidget::class.java)
            itemClickIntent.action = ACTION_ITEM_CLICK
            itemClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
            val itemClickPendingIntent = PendingIntent.getBroadcast(
                context, 0, itemClickIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
            views.setPendingIntentTemplate(R.id.one_time_list, itemClickPendingIntent)
            views.setPendingIntentTemplate(R.id.repeatable_list, itemClickPendingIntent)

            // 更新小部件
            appWidgetManager.updateAppWidget(appWidgetId, views)
        }

        const val ACTION_ITEM_CLICK = "com.xiaomi.shoppinglist.ACTION_ITEM_CLICK"
        const val EXTRA_ITEM_ID = "com.xiaomi.shoppinglist.EXTRA_ITEM_ID"
        const val EXTRA_ITEM_TYPE = "com.xiaomi.shoppinglist.EXTRA_ITEM_TYPE"
    }
}